//
//  SPSlidingViewController.m
//  QADoctor
//
//  Created by 陈飞宇 on 17/3/31.
//  Copyright © 2017年 Spencer. All rights reserved.
//

#import "SPSlidingViewController.h"

#import "SPSlidingCell.h"
#import "LZLoginViewController.h"
#import "AppDelegate.h"
#import "JNavigationController.h"


static NSString * const ID = @"cell";

@interface SPSlidingViewController ()
@property (nonatomic, weak) UIPageControl *control;
@property (nonatomic,strong)NSArray *imageArr;

@end

@implementation SPSlidingViewController

- (instancetype)init
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 设置cell的尺寸
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    
    // 清空行距
    layout.minimumLineSpacing = 0;
    
    // 设置滚动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return [super initWithCollectionViewLayout:layout];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    [ BaseRequest requestWithMethodResponseStringResult:TestURL paramars:nil paramarsSite:@"/Login/lead" sucessBlock:^(id content) {
        NSLog(@"content--%@",content);
        self.imageArr = content[@"list"];
        // 添加pageController
//        [self setUpPageControl];
        [self.collectionView reloadData];
    } failure:^(NSError *error) {
        
        AppDelegate *delegate =(AppDelegate *)[UIApplication sharedApplication].delegate;
        LZLoginViewController *loginVc = [[LZLoginViewController alloc]init];
        JNavigationController *nav = [[JNavigationController alloc]initWithRootViewController:loginVc];
        delegate.window.rootViewController = nav;
        
    }];
    
    // 注册cell,默认就会创建这个类型的cell
    [self.collectionView registerClass:[SPSlidingCell class] forCellWithReuseIdentifier:ID];
    
    // 分页
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    
    
    

    
}


// 添加pageController
- (void)setUpPageControl
{
    // 添加pageController,只需要设置位置，不需要管理尺寸
    UIPageControl *control = [[UIPageControl alloc] init];
    
    control.numberOfPages = self.imageArr.count;
    control.pageIndicatorTintColor = [UIColor blackColor];
    control.currentPageIndicatorTintColor = [UIColor redColor];
    
    // 设置center
    control.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height);
    _control = control;
    [self.view addSubview:control];
}
#pragma mark - UIScrollView代理
// 只要一滚动就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 获取当前的偏移量，计算当前第几页
    int page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;
    
    // 设置页数
    _control.currentPage = page;
}

// 返回有多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
// 返回第section组有多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArr.count;
}
// 返回cell长什么样子
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // dequeueReusableCellWithReuseIdentifier
    // 1.首先从缓存池里取cell
    // 2.看下当前是否有注册Cell,如果注册了cell，就会帮你创建cell
    // 3.没有注册，报错
    SPSlidingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    cell.backgroundColor =[UIColor whiteColor];
    
    NSString *imageName = [NSString stringWithFormat:@"%@%@",IconURL,self.imageArr[indexPath.row][@"img"]];
    
    cell.nameStr = imageName;
    
    [cell setIndexPath:indexPath count:self.imageArr.count];
    
    
    return cell;
    
}
- (NSArray *)imageArr
{
    if (!_imageArr) {
        _imageArr = [NSArray array];
    }
    return _imageArr;
    
}
@end
